//
//  ViewController.m
//  objc_property_t 的使用
//
//  Created by dllo on 16/7/15.
//  Copyright © 2016年 dllo. All rights reserved.
//



// 博客链接：http://www.jianshu.com/p/1ce700bc9a0f

#import "ViewController.h"
#import "CellOfNews.h"
#import "NewsViewModel.h"

#define newsReuseIdentifier @"CellOfNews"
#define SCREENWIDTH self.view.frame.size.width
#define SCREENHEIGHT self.view.frame.size.height

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonnull, nonatomic, strong) UITableView *tableView;

@property (nonnull, nonatomic, strong) NSArray *arrOfData;
@property (nonnull, nonatomic, strong) NewsViewModel *viewModel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createTableView];
    
    // 数据
    __weak typeof (UITableView) *tableView = _tableView;
    
    _viewModel = [[NewsViewModel alloc]init];
    [_viewModel getData:^(NSArray *arr) {
        _arrOfData = arr;
        [tableView reloadData];
    }];
    
}

- (void)createTableView {
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, SCREENWIDTH, SCREENHEIGHT - 20) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [_tableView registerClass:[CellOfNews class] forCellReuseIdentifier:newsReuseIdentifier];
    
}


#pragma mark - tableView delegate/dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arrOfData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CellOfNews *cell = [tableView dequeueReusableCellWithIdentifier:newsReuseIdentifier];
    [cell setModel:[_arrOfData objectAtIndex:indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 150;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
